<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Thanhtoan extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(array("url"));
        $this->load->model("HOADON");
        $this->load->model("CT_HOADON");
        $this->load->library('cart');
        $this->load->model('BaoKimPayment');
        $this->load->library('cart');
    }

    public function index() {
        $ho = $this->input->post('shipFName');
        $ten = $this->input->post('shipFName');
        $sonha = $this->input->post('shipFName');
        $phuong = $this->input->post('shipFName');
        $quan = $this->input->post('shipFName');
        $buuchinh = $this->input->post('shipFName');
        $email = $this->session->userdata('email');
        $phidichvu = $this->input->post('phidv');
        $tongtien = $this->cart->total();
        $thanhtoan = $this->input->post('thanhtoan');
        $hoadon = new Hoadon();


        $max = $hoadon->Max('so_hoadon');

        $hoadon->set_so_hoadon(++$max);
        $hoadon->set_email_khachhang($email);
        $hoadon->set_phi_dichvu($phidichvu);
        $hoadon->set_tong_tien($tongtien);
        $hoadon->set_hinhthuc_thanhtoan($thanhtoan);
        $hoadon->set_gh_ho_tendem($ho);
        $hoadon->set_gh_ten($ten);
        $hoadon->set_gh_sonha_tenduong($sonha);
        $hoadon->set_gh_phuong_xa($phuong);
        $hoadon->set_gh_quan_huyen($quan);
        $hoadon->set_gh_tinh_tp("Hồ Chí Minh");
        $hoadon->set_gh_ma_buuchinh($buuchinh);
        $hoadon->set_gh_sdt($this->input->post('phone_bill'));
        $hoadon->set_trangthai_giaohang(0);
        $today = date("Y-m-d H:i:s");
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $hoadon->set_ngaylap_hoadon($today);
        $hoadon->Save();

        ///  Sơn insert chi tiết giỏ hàng tại đây
        //
        $giohang = $this->cart->contents();
        foreach ($giohang as $hang) {
            $cthd = new Ct_hoadon();
            $cthd->set_dongia($hang['price']);
            $cthd->set_masp($hang['id']);
            $cthd->set_soluong($hang['qty']);
            $cthd->set_so_hoadon($hoadon->get_so_hoadon());
            $cthd->Save();
        }
        // xoa gio hang khi da them vao chi tiet
        $this->cart->destroy();



        ///
        // chuyển hướng nếu thanh toán trực tuyến
        if ($thanhtoan == "Thông qua bảo kim") {
            $bk = new BaoKimPayment();
            $tongtien=$tongtien+$phidichvu;
            $a = $bk->createRequestUrl($max, "longtrieuuit@gmail.com", $tongtien, null, null, "DL_TEST", "http://longtrieuuit.tk/baokim/baokim.php", NULL, NULL);
            redirect($a);
        } else {
            redirect(base_url('index.php/welcome/lichsugd'));
        }
    }

    public function in() {
        if (isset($_GET['so_hoadon'])) {
            $so_hoadon = $_GET['so_hoadon'];
            $hoadon = new Hoadon();
            $hoadon->getObject("so_hoadon=$so_hoadon");
            $this->load->library('Mpdf');
            $mpdf = new Pdf();
            // $html = "Vo Minh";
            $mpdf->SetAutoFont(AUTOFONT_ALL);
            $mpdf->SetHeader('AIOShop.vn||{PAGENO}');
            $this->load->model("ct_hoadon");
            $cthd = new Ct_hoadon();
            $dt = array();
            $dt['phidichvu']=$hoadon->get_phi_dichvu();
            $dt['mpdf'] = $mpdf;
            $dt['cthd'] = $cthd->getListObjectForCthd($so_hoadon);
            $dt['so_hoadon'] = $so_hoadon;
            $dt['hoten']= $hoadon->get_gh_ho_tendem()." ".$hoadon->get_gh_ten();
            $dt['diachi']=$hoadon->get_gh_sonha_tenduong()."-".$hoadon->get_gh_phuong_xa()."-".$hoadon->get_gh_quan_huyen()."-".$hoadon->get_gh_tinh_tp();
            $this->load->view("pdf_cthd", $dt);
        }
        else
            echo"phai chon 1 so_hoadon";
    }

}
